package com.zsl.swing.redis.desktop.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class TextFileWriter {

    private BufferedWriter writer;

    public TextFileWriter(String filename) throws IOException {

        File file = new File(filename);

        if(file.exists()){
            file.delete();
        }

        file.createNewFile();

        this.writer = new BufferedWriter(new FileWriter(file));
    }

    public TextFileWriter append(String line) throws IOException {
        this.writer.write(line);
        this.writer.newLine();
        return this;
    }

    public TextFileWriter append(List<String> lineList) throws IOException {
        for(String line: lineList){
            this.writer.write(line);
            this.writer.newLine();
        }

        return this;
    }


    public void write(){
        try{
            this.writer.flush();
            this.writer.close();
        }catch (Exception e){
            e.printStackTrace();
        }

    }


    public static void generateTxtFile(String fileName, List<String> sqlList) throws Exception{
        TextFileWriter b = new TextFileWriter(fileName);
        b.append(sqlList);

        b.write();
    }
}
